package com.czxy.demo05;

/**
 * Created by hp on 2020/2/2.
 */
public class TicketThread implements Runnable {
    private Ticket t;

    @Override
    public String toString() {
        return "TicketThread{" +
                "t=" + t +
                '}';
    }

    public Ticket getT() {
        return t;
    }

    public void setT(Ticket t) {
        this.t = t;
    }

    public TicketThread(Ticket t) {

        this.t = t;
    }

    public TicketThread() {

    }

    @Override
    public void run() {
        while (t.getTicket()>0){
            synchronized (this){
                if (t.getTicket()>0){
                    t.setTicket(t.getTicket()-1);
                    System.out.println(Thread.currentThread().getName()+"号窗口出票成功，现在余票"+t.getTicket()+"张");
                }try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
